package com.jiamao.goplay.ui.msg.vm;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppApplication;
import com.jiamao.goplay.bean.BaseRep;
import com.jiamao.goplay.bean.NearbyItem;
import com.jiamao.goplay.bean.PaifItme;
import com.jiamao.goplay.bean.res.ResMessage;
import com.jiamao.goplay.bean.res.ResNearbyList;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.data.BaseSubscriber;
import com.jiamao.goplay.data.DataRepository;
import com.jiamao.goplay.ui.discover.vm.NeayByItemVM;
import com.jiamao.goplay.ui.discover.vm.NeayByVM;
import com.jiamao.goplay.ui.user.OthersUserInfoFragment;
import com.jiamao.goplay.ui.user.UserPairFragment;
import com.jiamao.goplay.util.StringUtils;
import com.tamsiree.rxkit.view.RxToast;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class MsgHomeVM extends BaseViewModel<DataRepository> {

    public ObservableField<String> pdNum = new ObservableField<>("共0个配对");

    public MsgHomeVM(@NonNull Application application, DataRepository repository) {
        super(application, repository);
//        MsgItemVM item = new MsgItemVM(this);
//        item.multiItemType("add");
//        observableList.add(item);
//        //模拟10个条目，数据源可以来自网络
//        for (int i = 0; i < 5; i++) {
//
//                MsgItemVM item1 = new MsgItemVM(this);
//            item1.multiItemType("item");
//                observableList.add(item1);
//
//        }

    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Integer> pSwitchEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Integer> refreshEvent = new SingleLiveEvent<>();

    }
    //给RecyclerView添加ObservableList
    public ObservableList<MultiItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            //通过item的类型, 动态设置Item加载的布局
            String itemType = (String) item.getItemType();
            if (itemType.equals("add")){
                itemBinding.set(BR.viewModel, R.layout.msg_home_item_add_layout);
            }else {
                itemBinding.set(BR.viewModel, R.layout.msg_home_item_layout);
            }

        }
    });

    public void getLoadData(){

        model.getApi().messageList()
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider())) // 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())  // 线程调度
                //   .compose(RxUtils.exceptionTransformer())   // 网络错误的异常转换
                //    .doOnSubscribe(this) //请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        uc.refreshEvent.setValue(1);
                        //      showDialog();
                        // dialog.show();
                    }
                })
                .subscribe(new BaseSubscriber<BaseRep<ResMessage>>() {
                    @Override
                    public void onNext(BaseRep<ResMessage> baseRep) {
                        super.onNext(baseRep);
                        uc.refreshEvent.setValue(0);
                        if (baseRep.isOk()){

                            observableList.clear();
                            if (baseRep.getData().getPairList()==null){
                                return;
                            }


                            pdNum.set("配对：共 "+ StringUtils.formatBigNum(baseRep.getData().getPairNum())+" 个配对");
                            MsgItemVM numsItem = new MsgItemVM(MsgHomeVM.this,baseRep.getData().getInviteInfo());
                            numsItem.multiItemType("add");
                            observableList.add(numsItem);
                            for (ResMessage.ParfItme item: baseRep.getData().getPairList()){
                                MsgItemVM items = new MsgItemVM(MsgHomeVM.this,item);
                                items.multiItemType("item");
                                observableList.add(items);
                            }
                        }else {
                            uc.refreshEvent.setValue(2);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        //  dismissDialog();
                        uc.refreshEvent.setValue(2);
                        e.printStackTrace();
                     //   RxToast.showToastShort(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        //   dismissDialog();
                    }

                });
    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            startContainerActivity(UserPairFragment.class.getCanonicalName());
            // ToastUtils.showShort("我是头布局");
        }
    });
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
